$(function () {
    $('#Commontop').load('../assets/public/html/PublicLoginNotLogIn.html');

    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

    function errorWindows(text1, text2) {
        toastr.error(`${text2}`, `${text1}`)
    }

    function successWindows(text1, text2) {
        toastr.success(`${text2}`, `${text1}`)
    }

    let li = $('.con_top ul li');
    let num = 1;

    //点击切换
    // for (var i = 0; i < li.length; i++) {
    //     li[i].id = i;
    //     li[i].onclick = function () {
    //         let idx = this.id;
    //
    //         $('.cb_box').css({
    //             'right': idx * 1075 + 'px'
    //         })
    //         $(this).css('color', '#FF511C').siblings().css('color', '#d1d1d1');
    //     }
    // }

    //确认手机号
    $('#p_Retrieve').click(function () {
        const pipt = $(this).prevAll('input');
        if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(pipt.val()))) {
            $('#bindPhoneError').text("手机号码有误，请重填");
            return false;
        } else {
            $('#bindPhoneError').text('')
            $('.cb_box').css({
                'right': 1 * 1075 + 'px'
            })
            $('.con_top ul li').eq(1).css('color', '#FF511C').siblings().css('color', '#d1d1d1');
        }
    })

    //发送验证码
    let sendTime = 60;

    //限制短信发送时间
    limitedTime = function (dom) {
        if (sendTime == 0) {
            dom.css({'pointer-events': 'auto'})
            dom.prop('disabled', false)
            dom.text('获取验证码');
            $('.cb_sendbtn').css('background','#ff511c')
            sendTime = 60
            return
        } else {
            sendTime--
            $('.cb_sendbtn').css('background','darkgray')
            dom.css({'pointer-events': 'none'})
            dom.prop('disabled', true)
            dom.text(`${sendTime}秒后重新发送`)
        }
        setTimeout(() => {
            limitedTime(dom)
        }, 1000)
    }

    $('.cb_sendbtn').click(function () {
        const send = Interceptor('/send', 'post', {
            'phone': $('#RetrievePhone').val()
        })
        const {code, message} = send
        code != 666 ? errorWindows(message, '') : successWindows(message, '')
    })

    //判断手机号和验证码
    $('#verifyBtn').click(() => {
        const shortRule = /^[0-9]{4}$/;
        let shortFlag = false;
        if (!shortRule.test($('#RetrieveCode').val())) {
            $('#codeError').text('验证码格式不正确')
        } else {
            shortFlag = true;
        }
        if (shortFlag) {
            console.log(typeof parseInt($('#RetrievePhone').val()));
            console.log(typeof parseInt($('#RetrieveCode').val()));

            let verify = Interceptor('/verifyPhoneCode', 'post', {
                'phone': parseInt($('#RetrievePhone').val()),
                'Code': parseInt($('#RetrieveCode').val())
            })
            const {code, message} = verify
            if (code == 304) {
                successWindows(message, '')
                $('.cb_box').css({
                    'right': 2150 + 'px'
                })
                $('.con_top ul li').eq(2).css('color', '#FF511C').siblings().css('color', '#d1d1d1');
            } else {
                errorWindows(message, '')
            }
        }
    })

    //判断密码
    $('#RetrievePassBtn').click(() => {
        let passRule = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/;
        let RetrieveNewPass = $('#RetrieveNewPass').val();
        let RetrieveNewRePass = $('#RetrieveNewRePass').val()
        let passFlag = false;
        let repassFlag = false;
        if (!passRule.test(RetrieveNewPass)) {
            $('#PassError').text('密码格式错误')
        } else {
            passFlag = true;
        }

        if (RetrieveNewRePass != RetrieveNewPass) {
            $('#rePassError').text('密码不一致')
        } else {
            repassFlag = true;
        }

        if (passFlag && repassFlag) {
            let passRetrieve = Interceptor('/passRetrieve', 'post', {
                'phone': $('#RetrievePhone').val(),
                'password': RetrieveNewPass,
                'rePassword': RetrieveNewRePass
            })
            const {code, message} = passRetrieve
            if (code == 304) {
                $('.circle').attr('class', 'circle circleAnimation')
                $('.tick').attr('class', 'tick tickAnimation')
                $('.successText').addClass('succesTextAnimation')
                $('.cb_box').css({'right': 3225 + 'px'})
                $('.con_top ul li').eq(3).css('color', '#FF511C').siblings().css('color', '#d1d1d1');
                successWindows(message, '3秒后跳转登录页')
                setTimeout(() => {
                    window.location.replace('Login.html')
                }, 4000)
            } else {
                errorWindows(message, '')
            }
        }
    })
})